from BeautifulSoup import BeautifulSoup as bs
import re, os

f = open('d:/h.html')
s = f.read()
f.close()

b = bs(s).findAll('a', attrs={'class': 'J-media-item'})

d = {}
for i in b:
    a = re.match(r'(\d+-\d+)\s*(.+?)\s*\n', i.text)
    d[a.group(2)] = a.group(1)

print len(d)
p = r'D:\Document_x64\Downloads\Video\1'
os.chdir(p)
for i in os.listdir(p):
    a = i.decode('gb2312')
    b = a[:-4]
    if b in d:
        os.rename(a, d[b] + ' ' + a)
        del d[b]
    else:
        print a

for i in d:
    if not os.path.exists(d[i]+' '+i):
        print d[i], i
